<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Cache;
use org\lucn\Lucn;

class Nav extends Controller {
    const DB = 'lucn30';
	const PRE = NULL;
    const TB1 = 'favorite_list';
	const TB2 = 'favorite_class';
	
	public function ajax_clear(){
	    Cache::rm('favorite_category');
		Cache::rm('favorite_list');
		$obj = [];
		$obj['a'] = 1;
		$obj['b'] = '清理缓存成功';
		echo json_encode($obj);
	}
	
    public function index(){
	    if(!$list1 = Cache::get('favorite_category')){
	    	$list1 = DB::table(self::TB2)->order('sort_order asc')->select();
			Cache::set('favorite_category',$list1);
		}
		if(!$list2 = Cache::get('favorite_list')){
		$list2 = array();
		    foreach($list1 as $i => $son){
		        $where['class'] = $son['id'];
		        $list2[$i] = DB::table(self::TB1)->where($where)->select();
		    }
		    
			Cache::set('favorite_list',$list2);
		}
		//dump($list2);die();
		$this->assign('db','lucn30');
		$this->assign('tb1',self::TB1);
		$this->assign('tb2',self::TB2);
		$this->assign('list1',$list1);
		$this->assign('list2',$list2);
        return $this->Fetch('/nav_index');
    }
	
	public function update(){
	    $db = input('get.db');
		$tb = input('get.tb');
		$post= input('post.');
		if($db && $tb){
		$arr = explode('|',$post['field']);$i=0;
		foreach($arr as $i => $son){
		   if($son != 'id'){
		    $data[$son] = $post[$son];
			}
		}
		$where['id'] = $post['id'];
		$r = DB::table(self::TB1)->where($where)->update($data);
		//dump($where);die;
		if($r){
		    $a = 1;$b = "修改成功";}else{$a = 0;$b = "update数据失败。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>SQL:".M($tb,self::PRE,"mysql://root:123456@localhost:3306/".$db)->getLastSql();
		}

        }else{$a = 0;$b = "库和表不能为空。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>";}
		echo json(array('a'=>$a,'b'=>$b));
	}

	public function update_class(){
	    $db = input('get.db');
		$tb = input('get.tb');
		$post= input('post.');
		if($db && $tb){
		$arr = explode('|',$post['field']);$i=0;
		foreach($arr as $i => $son){
		   if($son != 'id'){
		    $data[$son] = $post[$son];
			}
		}
		$where['id'] = $post['id'];
		$r = DB::table(self::TB2)->where($where)->update($data);
		//dump($where);die;
		if($r){
		    $a = 1;$b = "修改成功";}else{$a = 0;$b = "update数据失败。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>SQL:".M($tb,self::PRE,"mysql://root:123456@localhost:3306/".$db)->getLastSql();
		}

        }else{$a = 0;$b = "库和表不能为空。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>";}
		echo json(array('a'=>$a,'b'=>$b));
	}

	public function insert(){
	    $db = input('get.db');
		$tb = input('get.tb');
		$post= input('post.');
		if($db && $tb){
		$arr = explode('|',$post['field']);$i=0;
		foreach($arr as $i => $son){
		   if($son != 'id'){
		    $data[$son] = $post[$son];
			}
		}
		unset($data['id']);
		$r = DB::table(self::TB1)->insert($data);
		//dump($where);die;
		if($r){
		    $a = 1;$b = "添加成功";}else{$a = 0;$b = "update数据失败。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>SQL:".M($tb,self::PRE,"mysql://root:123456@localhost:3306/".$db)->getLastSql();
		}

        }else{$a = 0;$b = "库和表不能为空。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>";}
		echo json(array('a'=>$a,'b'=>$b));
	}

	public function insert_class(){
	    $db = input('get.db');
		$tb = input('get.tb');
		$post= input('post.');
		if($db && $tb){
		$arr = explode('|',$post['field']);$i=0;
		foreach($arr as $i => $son){
		   if($son != 'id'){
		    $data[$son] = $post[$son];
			}
		}
		unset($data['id']);
		$r = DB::table(self::TB2)->insert($data);
		//dump($where);die;
		if($r){
		    $a = 1;$b = "添加成功";}else{$a = 0;$b = "update数据失败。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>SQL:".M($tb,self::PRE,"mysql://root:123456@localhost:3306/".$db)->getLastSql();
		}

        }else{$a = 0;$b = "库和表不能为空。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>";}
		echo json(array('a'=>$a,'b'=>$b));
	}
}